package cn.jdy.plugins.service.xysgajM;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.plugins.entity.xysgajM.Reports;
import cn.jdy.platform.service.ConfService;

/**
 * @author wuzhiliang
 * @title : 文章操作实现
 */
@Service
public class ReportsService extends BaseDao<Reports> {

	@Autowired
	private ConfService confService;

	/**
	 * 按条件查询
	 * @param Reports
	 * @return
	 */
	public List<Reports> query(Reports Reports, String... date) {
		if(Reports == null) return super.findAll();
		//return super.find(createFilter(Reports, Reports.getiDisplayLength() > 0, date));
		if(Reports.getiDisplayLength() > 0){
			return super.find(createFilter(Reports, date),Reports.getiDisplayStart(),Reports.getiDisplayLength());
		}
		return super.find(createFilter(Reports, date));
	}

	
	public Long count(Reports Reports, String... date) {
		return super.count(createFilter(Reports, date));
	}
	/**
	 * 拼装条件
	 * @param Reports
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(Reports Reports, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(!StringUtil.isNullOrEmpty(Reports.getKeywords())) {
      if(!StringUtil.isNullOrEmpty(Reports.getKeyType())) {
        dc.add(Restrictions.like(Reports.getKeyType(), Reports.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
		  dc.add(Restrictions.between("falseCreateTime", date[0],date[1]));
	  }
	  if(!StringUtil.isNullOrEmpty(Reports.getStatus())){
	    dc.add(Restrictions.eq("status", Reports.getStatus()));
	  }
	  
	  if(!StringUtil.isNullOrEmpty(Reports.getUserid())){
      dc.add(Restrictions.eq("userid", Reports.getUserid()));
      }
	  
	  //举报管理一级审核拼装语句
	  if(!StringUtil.isNullOrEmpty(Reports.getAudit1Status())){
		  dc.add(Restrictions.eq("audit1Status", Reports.getAudit1Status()));
	  }
	  //举报管理二级审核拼装语句
	  if (!StringUtil.isNullOrEmpty(Reports.getAudit2Status())) {
		  dc.add(Restrictions.eqOrIsNull("audit2Status", Reports.getAudit2Status()));
	  }
	  if(!StringUtil.isNullOrEmpty(Reports.getSortType())) {
	    dc.addOrder(Order.desc("sortType"));
	  }
	  //新增去掉投稿，即状态=0的news
	 // dc.add(Restrictions.or(Restrictions.eq("status", "1"),Restrictions.eq("status", "2"),Restrictions.eq("status", "3"),Restrictions.eq("status", "4"),Restrictions.eq("status", "5"),Restrictions.eq("status", "6"),Restrictions.eq("status", "7")));

	  dc.addOrder(Order.desc("falseCreateTime"));
		return dc;
	}

	/**
	 * 查询热门新闻
	 * @return
	 */
	public List<Reports> queryHotNews() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.add(Restrictions.eq("status", "1"));//前台查询需展示有效状态文章
    dc.addOrder(Order.desc("readTimes"));
    return super.find(dc,0,5);
		
	}
	
	/**
	 * @since 040116
	 * 模糊查询新闻
	 * @return
	 * 加入columnId
	 */
	public List<Reports> queryByLike(Reports Reports,String keywords) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.add(Restrictions.eq("status", Reports.getStatus()));//前台查询需展示有效状态文章
	  dc.add(Restrictions.or(
	      Restrictions.like("title", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("keyword", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("summary", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("context", keywords, MatchMode.ANYWHERE)
	  ));
//	  if(Reports.getColumnId()!=null){
//		  dc.add(Restrictions.eq("columnId", Reports.getColumnId()));
//	  }
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc,Reports.getiDisplayStart(),Reports.getiDisplayLength());
	}
	
	/**
	 * 模糊查询新闻
	 * @return
	 */
	public int queryByLikeCount(Reports Reports,String keywords) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.add(Restrictions.eq("status", Reports.getStatus()));//前台查询需展示有效状态文章
	  dc.add(Restrictions.or(
	      Restrictions.like("title", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("keyword", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("summary", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("context", keywords, MatchMode.ANYWHERE)
	  ));
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc).size();
	}

	/**
	 * 按照类型查询 文章
	 * @return
	 */
	public List<Reports> findbytype(Long columnid,int firstResult, int maxResults, boolean isImg) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
//	  前台查询需展示有效/置顶状态的文章
	  dc.add(Restrictions.or(Restrictions.eq("status", "1"),Restrictions.eq("status", "5"),Restrictions.eq("status", "6"),Restrictions.eq("status", "7")));
//	  dc.add(Restrictions.eq("status", "1"));//前台查询需展示有效状态文章
	  if(null != columnid){
	    dc.add(Restrictions.eq("columnId", columnid));
	  }
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
	  }
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc, firstResult, maxResults);
	}
	
	/**
	 * 按照大分类 查询 文章
	 * @return
	 */
	public List<Reports> findbyPid(Long pid,int firstResult, int maxResults, boolean isImg) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.createAlias("cv", "cv");
//	  前台查询需展示有效/置顶状态的文章
//	  dc.add(Restrictions.or(Restrictions.eq("status", "1"),Restrictions.eq("status", "3")));
	  dc.add(Restrictions.eq("status", "1"));//前台查询需展示有效状态文章
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
    }
	  if(null != pid){
	    dc.add(Restrictions.eq("cv.pid", pid));
	  }
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc,firstResult,maxResults);
	}
	
	/**
	 * 站点地图
	 * @return
	 */
	public List<Map<String, String>> querySiteMap(int start,int limit) {
	  String sql = "select id,title,createTime from "+entityClass.getSimpleName()+" where status =1 limit ?,?";
    SQLQuery sqlQuery = super.em().createSQLQuery(sql);
    sqlQuery.setParameter(0, start);
    sqlQuery.setParameter(1, limit);
    // 将查询寻结果转换为Map格式，解决多表查询或零散字段查询 映射取值问题
    sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    return sqlQuery.list();
	}
 
	/**
	 * 查询置顶新闻
	 * @return
	 */
	public List<Reports> queryStickieNews() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.add(Restrictions.eq("status", "3"));//查询置顶状态的文章
    dc.addOrder(Order.desc("readTimes"));
    return super.find(dc,0,5);
	}
	
	/**
	 * 按照类型查询 文章
	 * @return
	 */
	public List<Reports> findbytypeNstatus(Long columnid,int firstResult, int maxResults, boolean isImg, int isStickied) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
//	  前台查询需展示有效/置顶状态的文章
//	  dc.add(Restrictions.or(Restrictions.eq("status", "1"),Restrictions.eq("status", "3")));
	  dc.add(Restrictions.eq("status", "3"));//仅查询置顶状态-实际上仅一条
	  if(null != columnid){
	    dc.add(Restrictions.eq("columnId", columnid));
	  }
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
	  }
	  dc.addOrder(Order.desc("falseCreateTime"));
	  return super.find(dc, firstResult, maxResults);
	}
	
}